package ciyuan.Tasks;

import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;

import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Mod.EventBusSubscriber
public class TaskManager {

    private static final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(20);
    private static final Vector<Runnable> tasks = new Vector<>();

    public static void addTask(Runnable runnable) {
        tasks.add(runnable);
    }

    public static void removeTask(Runnable runnable) {
        tasks.remove(runnable);
    }

    @SubscribeEvent
    public static void onServerStarted(FMLServerStartedEvent event) {
        scheduledThreadPool.scheduleWithFixedDelay(() -> {
                    tasks.forEach(scheduledThreadPool::execute);
                }, 1000L, 1000L, TimeUnit.MILLISECONDS
        );
    }

}
